package com.supply.dl.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.BindView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.supply.dl.R;
import com.supply.dl.app.http.BaseInfo;
import com.supply.dl.bean.BaseItemBean;
import com.supply.dl.bean.ChangeHouseConfirmBean;
import com.supply.dl.bean.request.ChangeHouseConfirmRequest;
import com.supply.dl.http.HttpManger;
import com.supply.dl.utils.CommonNormalAdapterUtil;
import com.supply.dl.utils.RecyclerViewUtil;
import com.supply.dl.widget.LoadTipFailedLayout;
import com.supply.dl.widget.adapter.CommonNormalAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 移库收货确认列表页面
 */
public class ChangeAndGetGoodsNotifyListActivity extends BaseActivity
{
	List<ChangeHouseConfirmBean> changeAndGetGoodsBeanList = new ArrayList<ChangeHouseConfirmBean>();
	/**
	 * 搜索内容输入框
	 */
	@BindView(R.id.common_search_main_searchContentEdt) EditText editSearch;

	/**
	 * 搜索按钮
	 */
	@BindView(R.id.common_search_main_searchTv) TextView txtSearch;

	/**
	 * 列表显示
	 */
	@BindView(R.id.swipe_target) RecyclerView recyclerView; //修改了id
	@BindView(R.id.layout_load_failed) LoadTipFailedLayout loadTipFailedLayout;
	CommonNormalAdapter commonNormalAdapter;
	List<BaseItemBean> itemList = new ArrayList<BaseItemBean>();

	@Override protected int getLayoutResource()
	{
		return R.layout.activity_change_depot_take_delivery;
	}

	@Override protected void onCreate(@Nullable Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setTitleShow(getResourcesString(R.string.transfer_warehouse_receive_goods_confirm));
		showLeftBack();
		loadTipFailedLayout.setRestartOnClickListener(this);
		txtSearch.setOnClickListener(this);
		initData();
	}

	@Override protected void initRecyclerView()
	{
		super.initRecyclerView();
		RecyclerViewUtil.initFullLinearLayoutVERTICAL(this, recyclerView);

		commonNormalAdapter = CommonNormalAdapterUtil.initCommonNormalAdapter(R.layout.item_change_take_delivery, itemList, CommonNormalAdapter.FLAG_CHANGE_AND_GET_NOTIFY_LIST_MAIN);
		recyclerView.setAdapter(commonNormalAdapter);

		commonNormalAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener()
		{
			@Override public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i)
			{
				switch (view.getId())
				{
				case R.id.layout_root:

					Intent intent = new Intent(ChangeAndGetGoodsNotifyListActivity.this, ChangeHouseAndConfirmDetailActivity.class);
					intent.putExtra(ChangeHouseAndConfirmDetailActivity.NOTICE_ORDER_NUM, changeAndGetGoodsBeanList.get(i).noticeOrderId);
					intent.putExtra("ChangeHouseConfirmBean", changeAndGetGoodsBeanList.get(i));
					startActivity(intent);
					break;
				default:
					break;
				}
			}
		});
	}

	public void initData()
	{
		loadTipFailedLayout.setState(LoadTipFailedLayout.STATE_PROGRESS);
		HttpManger httpManger = HttpManger.getInstance();
		httpManger.setContext(this);
		ChangeHouseConfirmRequest changeAndGetRequest = new ChangeHouseConfirmRequest();
		changeAndGetRequest.key = editSearch.getText().toString().trim();
		httpManger.getChangeAndGetList(changeAndGetRequest);
		httpManger.setHttpResponseCallBackListener(new HttpManger.HttpResponseCallBackListener()
		{
			@Override public void onSuccess(BaseInfo<?> baseInfo)
			{
				changeAndGetGoodsBeanList = (List<ChangeHouseConfirmBean>) baseInfo.getData();
				itemList.clear();
				if (null != changeAndGetGoodsBeanList && changeAndGetGoodsBeanList.size() > 0)
				{
					loadTipFailedLayout.setState(LoadTipFailedLayout.STATE_FINISH);
					for (ChangeHouseConfirmBean locationBean : changeAndGetGoodsBeanList)
					{
						itemList.add(new BaseItemBean(locationBean));
					}
				}
				else
				{
					loadTipFailedLayout.setState(LoadTipFailedLayout.STATE_NORMAL);
				}
				commonNormalAdapter.setNewData(itemList);
			}

			@Override public void onFailure(String message)
			{
				showToast(message);
				loadTipFailedLayout.setState(LoadTipFailedLayout.STATE_FAILED);
			}
		});
	}

	@Override public void onClick(View v)
	{
		super.onClick(v);
		switch (v.getId())
		{
		case R.id.txt_load_failed:
			initData();
			break;
		case R.id.common_search_main_searchTv:
			initData();
			break;
		}
	}
}
